﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SlimDX;
namespace ChaosEngine.EngineCore.Graph
{
    public class TransformGraph : NodeGraph
    {
        public TransformGraph(string name) : base(name)
        {
                     

        }

        public override GraphNode CreateRoot(){

            return new TransformNode(Name + "_root", null);

        }

    }

    public class CameraNode : TransformNode
    {
             

        public new Matrix WorldTransform{

            get
            {

                return Matrix.Invert(base.WorldTransform);

            }

        }

        public CameraNode(string name,TransformNode parent) : base(name,parent)
        {


        }

    }
    
    public enum TransformSpace
    {
        World, Local
    }

    public class TransformNode : GraphNode
    {
        
      

        public Matrix Rotation { get; set; }
        public Vector3 Position {get;set;}
        public Vector3 Scale { get; set; }
        public Matrix WorldTransform{

            get
            {

                return Matrix.Scaling(Scale)*Matrix.Translation(Position) * Rotation;

            }

        }
        public Vector3 WorldPosition{

            get
            {

                var tmp = WorldTransform.get_Rows(3);
                return new Vector3(tmp.X, tmp.Y, tmp.Z);
            }

        }


        public TransformNode(string name,TransformNode parent) : base(name,parent)
        {

            Rotation = Matrix.Identity;
            Position = Vector3.Zero;
            Scale = new Vector3(1.0f, 1.0f, 1.0f);
        }

        public TransformNode Rotate(Vector3 rot,TransformSpace space)
        {

            switch (space)
            {
                case TransformSpace.Local:

                    Rotation = Matrix.RotationYawPitchRoll(rot.X, rot.Y, rot.Z);
                    
                    break;
                case TransformSpace.World:
                
                    throw new NotImplementedException("World Rotation Not Yet Implemented.");
             
            }

            return this;

        }

        public TransformNode Turn(Vector3 rot,TransformSpace space)
        {

            switch (space)
            {
                case TransformSpace.Local:

                    Rotation = Rotation * Matrix.RotationYawPitchRoll(rot.X, rot.Y, rot.Z);
                    
                    break;
                case TransformSpace.World:
                
                    throw new NotImplementedException("World Rotation Not Yet Implemented.");
             
            }

            return this;

        }

        public TransformNode SetPos(Vector3 pos,TransformSpace space)
        {

            switch (space)
            {
                case TransformSpace.Local:

                    Position = pos;

                    break;
                case TransformSpace.World:

                    throw new NotImplementedException("World Positioning Not Yet Implemented.");

            }


            return this;

        }

    }

}
